package com.example;

import com.example.bean.ConsumerComponent;
import com.example.filter.TPMonitorFilter;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.time.Instant;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/2/27 20:42
 */
public class ConsumerApplication {

    public static void main(String[] args) throws Exception {
        // ClassPathXmlApplicationContext context =
        //         new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent bean = context.getBean(ConsumerComponent.class);
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        new Thread(ConsumerApplication::getResult).start();

        long start = Instant.now().toEpochMilli();
        for (int i = 0; i < 9200; i++) {
            executorService.submit(new HelloServiceThread(bean));
        }
        executorService.shutdown();
        executorService.awaitTermination(3, TimeUnit.MINUTES);
        long end = Instant.now().toEpochMilli();
        System.out.println("总耗时 = " + (end - start));
    }

    private static void getResult() {
        while (true) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                TPMonitorFilter.calculate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class HelloServiceThread extends Thread {

        private final ConsumerComponent helloService;

        HelloServiceThread(ConsumerComponent helloService) {
            super();
            this.helloService = helloService;
        }

        @Override
        public void run() {
            try {
                TimeUnit.MILLISECONDS.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            final int i = ThreadLocalRandom.current().nextInt(3);
            String result = null;
            switch (i) {
                case 0:
                    result = helloService.methodA(ThreadLocalRandom.current().nextInt(100));
                    break;
                case 1:
                    result = helloService.methodB(ThreadLocalRandom.current().nextInt(100));
                    break;
                case 2:
                    result = helloService.methodC(ThreadLocalRandom.current().nextInt(100));
                    break;
                default:
                    break;
            }
            // 调用结果
            // System.out.println("result = " + Objects.toString(result, ""));
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.example")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}

